package com.unionftech.common.common.language

import android.text.TextUtils
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.common.model.LanguageModel
import com.unionftech.common.utils.SpUtils

object LanguageDownloader {
    private val mModel by lazy { LanguageModel() }

    /**
     * 获取语言包
     */
    fun downloadLanguageJson(languageCode: String, callBack: ((Boolean) -> Unit)? = null) {
        mModel.getLanguage(languageCode, object : IBaseCallBack<ResponseResult<String?>> {
            override fun onResponse(response: ResponseResult<String?>) {
                if (response.code == ResponseResult.SUCCESS_CODE) {
                    if (!TextUtils.isEmpty(response.data)) {
                        saveLanguageJson(languageCode, response.data!!)
                        callBack?.invoke(true)
                        return
                    }
                }
                callBack?.invoke(false)
            }
        })
    }

    private fun saveLanguageJson(languageCode: String, languageJson: String) {
        SpUtils.getInstance().put(languageCode, languageJson)
    }
}